using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using AiPlatformAdmin.Api.Data;
using AiPlatformAdmin.Api.Models;
using AiPlatformAdmin.Api.Services;
using AiPlatformAdmin.Api.DTOs;

namespace AiPlatformAdmin.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class PluginsController : ControllerBase
{
    private readonly AppDbContext _context;
    private readonly ITenantService _tenantService;
    private readonly IMapper _mapper;

    public PluginsController(AppDbContext context, ITenantService tenantService, IMapper mapper)
    {
        _context = context;
        _tenantService = tenantService;
        _mapper = mapper;
    }

    [HttpGet]
    public async Task<ActionResult<IEnumerable<PluginDto>>> GetPlugins()
    {
        if (!_tenantService.CurrentTenantId.HasValue)
        {
            return Forbid();
        }

        var plugins = await _context.Plugins
            .Include(p => p.AgentPlugins)
                .ThenInclude(ap => ap.Agent)
            .ToListAsync();

        return Ok(_mapper.Map<IEnumerable<PluginDto>>(plugins));
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<PluginDto>> GetPlugin(int id)
    {
        if (!_tenantService.CurrentTenantId.HasValue)
        {
            return Forbid();
        }

        var plugin = await _context.Plugins
            .Include(p => p.AgentPlugins)
                .ThenInclude(ap => ap.Agent)
            .FirstOrDefaultAsync(p => p.Id == id);

        if (plugin == null)
        {
            return NotFound();
        }

        return Ok(_mapper.Map<PluginDto>(plugin));
    }

    [HttpPost]
    public async Task<ActionResult<PluginDto>> CreatePlugin(CreatePluginDto dto)
    {
        if (!_tenantService.CurrentTenantId.HasValue)
        {
            return Forbid();
        }

        var plugin = _mapper.Map<Plugin>(dto);
        plugin.CreatedAt = DateTime.UtcNow;
        plugin.UpdatedAt = DateTime.UtcNow;

        _context.Plugins.Add(plugin);
        await _context.SaveChangesAsync();

        var result = _mapper.Map<PluginDto>(plugin);
        return CreatedAtAction(nameof(GetPlugin), new { id = plugin.Id }, result);
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> UpdatePlugin(int id, UpdatePluginDto dto)
    {
        if (!_tenantService.CurrentTenantId.HasValue)
        {
            return Forbid();
        }

        var plugin = await _context.Plugins.FirstOrDefaultAsync(p => p.Id == id);
        if (plugin == null)
        {
            return NotFound();
        }

        _mapper.Map(dto, plugin);
        plugin.UpdatedAt = DateTime.UtcNow;

        await _context.SaveChangesAsync();
        return NoContent();
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> DeletePlugin(int id)
    {
        if (!_tenantService.CurrentTenantId.HasValue)
        {
            return Forbid();
        }

        var plugin = await _context.Plugins.FirstOrDefaultAsync(p => p.Id == id);
        if (plugin == null)
        {
            return NotFound();
        }

        _context.Plugins.Remove(plugin);
        await _context.SaveChangesAsync();

        return NoContent();
    }
}
